package com.totoro.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * Created by Niki on 2018/5/31 9:00
 */
public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();

        //设置30秒没有读到数据，触发一个READER_IDLE事件
        pipeline.addLast(new IdleStateHandler(30, 0, 0));
        //将请求和应答消息解码为HTTP消息
        pipeline.addLast("http-codec", new HttpServerCodec());
        //将HTTP消息的多个部分和成一条完整的HTTP消息
        pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
        //向客户端发送HTML5文件
        pipeline.addLast("http-chunked", new ChunkedWriteHandler());
        //在管道中添加我们自己的接收数据实现方法
        pipeline.addLast("handler", new MyWebSocketServerHandler());
    }
}
